﻿namespace com.stifel.ServerBuildMgmtDao
{
    using System.Collections.Generic;
    using System.Linq;

    using Microsoft.SharePoint.Linq;

    using Context = StifelServerBuildDataContext;

    using CustomObject = StifelOS;
    using System;

    public class StifelOSDao : StifelBaseDao
    {
        #region Fields

        public const string ListName = "stifelos";

        #endregion Fields

        #region Constructors

        public StifelOSDao( )
            : base( ListName )
        {
            //CurrentWebUrl = SPContext.Current.Web.Url;
            CurrentWebUrl = ConfiguredWebUrl;
        }

        public StifelOSDao( string currentWebUrl )
            : base( ListName )
        {
            this.CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public void Add( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                l.InsertOnSubmit( clean( value ) );
                c.SubmitChanges( );
            }
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public List<CustomObject> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                List<CustomObject> list = ( from o in l
                                            where o.Active == true
                                            select o ).ToList( );
                foreach ( CustomObject o in list )
                {
                    for ( int i = 0; i < o.RolesId.Count; i++ )
                    {
                        if ( o.RolesId[ i ].HasValue )
                        {
                            ServerRole serverRole = new ServerRole
                            {
                                Id = o.RolesId[ i ].Value,
                                Title = o.RolesTitle[ i ]
                            };
                            StifelOSRoleDao dao = new StifelOSRoleDao( CurrentWebUrl );
                            StifelOSRole osRole = dao.Get( serverRole.Id );
                            if ( osRole != null )
                                serverRole.Subroles = osRole.Subroles;
                            o.ServerRoles.Add( serverRole );
                        }
                    }
                }
                return list;
            }
        }

        public List<CustomObject> GetAllWithOutRoles( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                List<CustomObject> list = ( from o in l
                                            where o.Active == true
                                            select o ).ToList( );
                return list;
            }
        }

        public CustomObject Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.Id == id
                                   select o ).SingleOrDefault( );
                for ( int i = 0; i < v.RolesId.Count; i++ )
                {
                    if ( v.RolesId[ i ].HasValue )
                    {
                        ServerRole serverRole = new ServerRole
                        {
                            Id = v.RolesId[ i ].Value,
                            Title = v.RolesTitle[ i ]
                        };
                        StifelOSRoleDao dao = new StifelOSRoleDao( CurrentWebUrl );
                        StifelOSRole osRole = dao.Get( serverRole.Id );
                        if ( osRole != null )
                            serverRole.Subroles = osRole.Subroles;
                        v.ServerRoles.Add( serverRole );
                    }
                }
                return v;
            }
        }

        public CustomObject Get( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.UniqueId == uniqueId
                                   select o ).SingleOrDefault( );
                for ( int i = 0; i < v.RolesId.Count; i++ )
                {
                    if ( v.RolesId[ i ].HasValue )
                    {
                        ServerRole serverRole = new ServerRole
                        {
                            Id = v.RolesId[ i ].Value,
                            Title = v.RolesTitle[ i ]
                        };
                        StifelOSRoleDao dao = new StifelOSRoleDao( CurrentWebUrl );
                        StifelOSRole osRole = dao.Get( serverRole.Id );
                        if ( osRole != null )
                            serverRole.Subroles = osRole.Subroles;
                        v.ServerRoles.Add( serverRole );
                    }
                }
                return v;
            }
        }

        public CustomObject Get( string Title )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.Title.Equals(Title,StringComparison.InvariantCultureIgnoreCase)
                                   select o ).SingleOrDefault( );
                for ( int i = 0; i < v.RolesId.Count; i++ )
                {
                    if ( v.RolesId[ i ].HasValue )
                    {
                        ServerRole serverRole = new ServerRole
                        {
                            Id = v.RolesId[ i ].Value,
                            Title = v.RolesTitle[ i ]
                        };
                        StifelOSRoleDao dao = new StifelOSRoleDao( CurrentWebUrl );
                        StifelOSRole osRole = dao.Get( serverRole.Id );
                        if ( osRole != null )
                            serverRole.Subroles = osRole.Subroles;
                        v.ServerRoles.Add( serverRole );
                    }
                }
                return v;
            }
        }



        CustomObject clean( CustomObject value )
        {
            return new CustomObject
            {
                Active = value.Active,
                Default = value.Default,
                DftCPU = value.DftCPU,
                DftHardDriveLabel = value.DftHardDriveLabel,
                DftHardDriveName = value.DftHardDriveName,
                DftHardDrives = value.DftHardDrives,
                DftHDSizeGB = value.DftHDSizeGB,
                DftMemoryGB = value.DftMemoryGB,
                //DftNetworkConnections = value.DftNetworkConnections,
                InternalOSName = value.InternalOSName,
                LegacyOS = value.LegacyOS,
                MaxCPU = value.MaxCPU,
                MaxHardDrives = value.MaxHardDrives,
                //MaxHDSizeGB = value.MaxHDSizeGB,
                MaxMemoryGB = value.MaxMemoryGB,
                MaxNetworkConnections = value.MaxNetworkConnections,
                OSName = value.OSName,
                RolesId = value.RolesId,
                RolesTitle = value.RolesTitle,
                //Roles = value.Roles,
                Server = value.Server,
                Title = value.Title
            };
        }

        #endregion Methods
    }
}